package com.xl.basic.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xl.basic.entity.Result;
import com.xl.basic.entity.ResultEnums;
import com.xl.basic.entity.SPage;
import com.xl.basic.entity.param.QueryVisitLogParam;
import com.xl.basic.service.VisitLogService;
import com.xl.dao.domain.XlVisitLog;
import com.xl.dao.mapper.XlVisitLogMapper;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 钟勇
 * Create in 2023/8/24 10:10
 */
@Service
@RequiredArgsConstructor
public class VisitLogServiceImpl implements VisitLogService {

    private final XlVisitLogMapper visitLogMapper;

    @Override
    public Result<List<XlVisitLog>> queryVisitLogPage(@NonNull QueryVisitLogParam param, SPage sPage) {
        Page<XlVisitLog> page = visitLogMapper.selectPage(SPage.getPage(sPage), getQueryWrapper(param));
        return ResultEnums.Success.getPageResult(page);
    }

    /**
     * 生成查询列表wrapper
     *
     * @param param param
     * @return wrapper
     */
    private Wrapper<XlVisitLog> getQueryWrapper(@NonNull QueryVisitLogParam param) {
        return new LambdaQueryWrapper<XlVisitLog>()
                .and(StrUtil.isNotBlank(param.getActionKeyword()), w -> {
                    String actionKeyword = param.getActionKeyword();
                    w.like(XlVisitLog::getControllerName, actionKeyword)
                            .or()
                            .like(XlVisitLog::getActionName, actionKeyword)
                            .or()
                            .like(XlVisitLog::getControllerDescription, actionKeyword)
                            .or()
                            .like(XlVisitLog::getActionDescription, actionKeyword);

                })
                .and(StrUtil.isNotBlank(param.getVisitorKeyword()), w -> {
                    String visitorKeyword = param.getVisitorKeyword();
                    w.like(XlVisitLog::getClientIp, visitorKeyword)
                            .or()
                            .like(XlVisitLog::getUserId, visitorKeyword)
                            .or()
                            .like(XlVisitLog::getUserName, visitorKeyword);
                })
                .ge(param.getBeginTime() != null, XlVisitLog::getBeginTime, param.getBeginTime())
                .le(param.getEndTime() != null, XlVisitLog::getEndTime, param.getEndTime())
                .eq(param.getResponseCode() != null, XlVisitLog::getResponseCode, param.getResponseCode())
                .like(StrUtil.isNotBlank(param.getResponseMessageLike()), XlVisitLog::getResponseMessage, param.getResponseMessageLike())
                .eq(StrUtil.isNotBlank(param.getChannel()), XlVisitLog::getChannel, param.getChannel())
                .orderByDesc(XlVisitLog::getEndTime);
    }
}
